package com.scau402.system1.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.scau402.common.annotation.Log;
import com.scau402.common.core.controller.BaseController;
import com.scau402.common.core.domain.AjaxResult;
import com.scau402.common.core.page.TableDataInfo;
import com.scau402.common.enums.BusinessType;
import com.scau402.common.utils.SecurityUtils;
import com.scau402.system1.domain.dto.WmsWarehouseDTO;
import com.scau402.system1.domain.po.WmsWarehouseDO;
import com.scau402.system1.service.impl.WmsWarehouseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 仓库Controller
 *
 * @author ruoyi
 * @date 2023-06-26
 */
@RestController
@RequestMapping("/system/warehouse")
public class WmsWarehouseController extends BaseController {

    @Resource
    private WmsWarehouseService wmsWarehouseService;

    /**
     * 查询仓库列表
     */
    @PreAuthorize("@ss.hasPermi('system:warehouse:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody WmsWarehouseDTO wmsWarehouseDTO) {
        try (
                Page<Object> page = PageHelper.startPage(
                        wmsWarehouseDTO.getCurrentPage(), wmsWarehouseDTO.getPageSize()
                )
        ) {
            List<WmsWarehouseDO> list = wmsWarehouseService.list(wmsWarehouseDTO);
            return new TableDataInfo(list, page.getPageNum(), page.getPageSize(), page.getTotal());
        }
    }

    /**
     * 新增仓库
     */
    @PreAuthorize("@ss.hasPermi('system:warehouse:add')")
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsWarehouseDO wmsWarehouseDO) throws InterruptedException {
        wmsWarehouseService.add(wmsWarehouseDO);
        return success();
    }

    /**
     * 修改仓库
     */
    @PreAuthorize("@ss.hasPermi('system:warehouse:edit')")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsWarehouseDO wmsWarehouseDO) {
        wmsWarehouseDO.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        wmsWarehouseDO.setUpdateTime(new Date());

        wmsWarehouseService.updateWmsWarehouse(wmsWarehouseDO);
        return success();
    }

    /**
     * 删除仓库
     */
    @PreAuthorize("@ss.hasPermi('system:warehouse:remove')")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            wmsWarehouseService.deleteById(id);
        }
        return success();
    }

    /**
     * 查询仓库及其关联仓位
     * @return
     */
    @GetMapping("/getRelations")
    public AjaxResult getRelations(){
        return success(wmsWarehouseService.getRelations());
    }
}
